<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 * 商品管理
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$manage_op=array('start','off');
if(in_array($_RQ['op'],$manage_op)){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择商品！','error');
	}
}
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	if(!empty($_RQ['title'])){
		$params['title like']='%'.trim($_RQ['title']).'%';
		$return['params']['title']=$_RQ['title'];
	} 
	if(isset($_RQ['status'])){
		$params['status']=intval($_RQ['status']);
		$return['params']['status']=$_RQ['status'];
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$list=pdo_getslice('jy_pingdd_yafee_goods',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$item['url']=custom_url('jy_pingdd_yafee/goods.detail//id='.$item['goodsid'],true);
			$goods=pdo_get('jy_weishop_goods',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['goodsid']));
			$item['goodsprice']=$goods['price'];
			$item['title']=$goods['title'];
			$item['image']=$goods['image'];
			$item['endstatus']=$item['starttime']<time() ? $item['endtime']>time()? 1 : 2 : 0;
			$item['statustitle']=$item['starttime']<time() ? $item['endtime']>time()? '去拼团' : '已结束' : '未开始';
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>array(
			array('op'=>'list','title'=>'商品列表'),
			array('op'=>'edit','title'=>'添加商品'),
		),
		'list'=>array('data'=>'list','tip'=>'[温馨提示]:</br>1、不单独设置拼团信息的商品，将默认：设置--基本设置--里面默认设置！','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string'),
			array('name'=>'image','title'=>'图片','type'=>'image'),
			array('name'=>'goodsprice','title'=>'商品原价','type'=>'string'),
			array('name'=>'price','title'=>'拼团价格','type'=>'string'),
			array('name'=>'timeout','title'=>'组团限时/小时','type'=>'string'),
			array('name'=>'status','title'=>'是否启用','type'=>'switch','set'=>'set'),
			array('name'=>'url','title'=>'复制链接','type'=>'copy'),
			array('name'=>'url','title'=>'二维码','type'=>'ewm'),
		)),
		'search'=>array(
			array('name'=>'title','title'=>'商品名称','type'=>'string','width'=>'20'),
			array('name'=>'status','title'=>'是否启用','type'=>'select','options'=>'status','width'=>'20'),
		)
	);
	$return['status']=array(array('id'=>'0','title'=>'禁用'),array('id'=>'1','title'=>'启用'));
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='edit'){
	$return=array();
	$id=intval($_RQ['id']);
	if($id){
		$item=pdo_get('jy_pingdd_yafee_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$item=array('get_type'=>0);
	}
	$item['starttime']=$item['starttime']?date('Y-m-d H:i',$item['starttime']):'';
	$item['endtime']=$item['endtime']?date('Y-m-d H:i',$item['endtime']):'';
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'post'=>'post',
		'tabs'=>array(
			array('op'=>'list','title'=>'返回商品列表'),
		),
		'edit_tab'=>true,
		'edits'=>array(
						array(
							'title'=>'基本信息',
							'list'=>array(
								array('name'=>'goodsid','title'=>'选择商品','type'=>'choose_title','action'=>'jy_weishop/goods.get_goods/'),
								array('type'=>'unitstring','title'=>'拼团价格','name'=>'price','unit'=>'元'),
								array('type'=>'unitstring','title'=>'拼团定金','name'=>'ding_price','unit'=>'元'),
								array('type'=>'unitstring','title'=>'成团人数','name'=>'teamnum','unit'=>'人','help'=>''),
								array('type'=>'unitstring','title'=>'组团限时','name'=>'timeout','unit'=>'小时','help'=>'仅填数字，单位:小时'),
								array('type'=>'unitstring','title'=>'返现比例','name'=>'fan_rate','unit'=>'%'),
								array('type'=>'radio','title'=>'收货方式','name'=>'get_type','options'=>'get_type'),
							)
						),
			
			)
	);
	$return['get_type']=$this->get_types;
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='post'){
	$postdata=get_postdata();
	if(!$postdata['goodsid']){
		exi('请选择商品','error');
	}
	if(!$postdata['teamnum'] || $postdata['teamnum']<2){
		exi('成团人数必须大于等于2','error');
	}
	if($postdata['price']<=0){
		exi('拼团价格必填','error');
	}
	$data=array(
		'goodsid'=>intval($postdata['goodsid']),
		'timeout'=>intval($postdata['timeout']),
		'teamnum'=>intval($postdata['teamnum']),
		'fan_rate'=>intval($postdata['fan_rate']),
		'get_type'=>intval($postdata['get_type']),
		'ding_price'=>trim($postdata['ding_price']),
		'timeout'=>intval($postdata['timeout']),
		'status'=>intval($postdata['status']),
		'paixu'=>intval($postdata['paixu']),
		'price'=>trim($postdata['price']),
		'createtime'=>TIMESTAMP,
	);
	$id=intval($postdata['id']);
	if($id){
		pdo_update('jy_pingdd_yafee_goods',$data,array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}else{
		$data['uniacid']=$_SESSION['uniacid'];
		pdo_insert('jy_pingdd_yafee_goods',$data);
	}
	exi('操作成功！','','list');
}
if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_pingdd_yafee_goods',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='set'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要编辑的商品！','error');
	}
	$post=get_postdata();
	if(isset($post['status'])){
		pdo_update('jy_pingdd_yafee_goods',array('status'=>intval($post['status'])),array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
	}
	exi('操作成功！');
}
